package com.sllp.C71S3TsllpShop.web;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.multipart.MultipartFile;

import com.sllp.C71S3TsllpShop.bean.BigType;
import com.sllp.C71S3TsllpShop.bean.Car;
import com.sllp.C71S3TsllpShop.bean.Image;
import com.sllp.C71S3TsllpShop.bean.Orders;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.bean.User;
import com.sllp.C71S3TsllpShop.biz.impl.CarBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.ImageBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.OrdersBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.UserBizImpl;
import com.sllp.C71S3TsllpShop.dao.ImageMapper;

@Controller
public class UserAction {

	@Resource
	private UserBizImpl ubiz;
	@Resource
	private OrdersBizImpl obiz;
	@Resource
	private CarBizImpl cbiz;
	@Resource
	private ImageBizImpl imbiz;
	@Value("${spring.resources.staticLocations}")
	private String [] uploadDir;
	@Resource
	private ImageMapper im;
	
	@GetMapping("member")
	public String toUserMsg(HttpSession session,
			@SessionAttribute("btlist") List<List<BigType>> lists,
			@SessionAttribute("loginUser") User user,Model m){
		List<User> ulist=ubiz.userMsg(user);
		m.addAttribute("ulist", ulist);
		
		List<Orders> olist=obiz.findOrder(user);
		m.addAttribute("olist", olist);
		//获取到该用户不同状态的订单个数
    	int size1=olist.size();
    	int num1=0;
    	int num2=0;
    	int num3=0;
    	int num4=0;
    	int num5=0;
    	for(int i=0;i<size1;i++){
    		int size2=olist.get(i).getOrdersitem().size();
    		for(int j=0;j<size2;j++){
    			if(olist.get(i).getOrdersitem().get(j).getState()==1){
    				num1++;
    			}else if(olist.get(i).getOrdersitem().get(j).getState()==2){
    				num2++;
    			}else if(olist.get(i).getOrdersitem().get(j).getState()==3){
    				num3++;
    			}else if(olist.get(i).getOrdersitem().get(j).getState()==7){
    				num4++;
    			}else if(olist.get(i).getOrdersitem().get(j).getState()==8){
    				num5++;
    			}
    		}
    	}
    	m.addAttribute("num1", num1);
    	m.addAttribute("num2", num2);
    	m.addAttribute("num3", num3);
    	m.addAttribute("num4", num4);
    	m.addAttribute("num5", num5);
		return "Member";
	}
	
	/**
	 * 修改用户个人信息
	 */
	@GetMapping("updateMember")
	public String update(Model m,@SessionAttribute("loginUser") User user){
		int uid=user.getId();
		List<Image> imglist=imbiz.findImageByUser(uid);
		m.addAttribute("img", imglist);
		return "UpdateMember";
	}
	
	@ResponseBody
	@PostMapping("updateUserImg.do")
	public Result updateUserImg(Model m,@SessionAttribute("loginUser") User user,
			@RequestParam("userNewImage") MultipartFile file){
		String fileName="";
		try {
			String path= uploadDir[0].substring("file:/".length());
			String userFileName=file.getOriginalFilename();
			String suffix = userFileName.substring(userFileName.lastIndexOf('.'));
			String addFileName=String.valueOf(System.currentTimeMillis()).substring(6);
			fileName = "user/"+addFileName+suffix;
			
			//判断文件类型是否正确
			String contentType = file.getContentType();
			String result="";
			if (contentType.equals("image/pjpeg")
					|| contentType.equals("image/jpeg")) {
				// IE6上传jpg图片的headimageContentType是image/pjpeg，而IE9以及火狐上传的jpg图片是image/jpeg
			} else if (contentType.equals("image/png")
					|| contentType.equals("image/x-png")) {
				// IE6上传的png图片的headimageContentType是"image/x-png"
			} else if (contentType.equals("image/gif")) {
			} else if (contentType.equals("image/bmp")) {
			} else {
				return new Result(0, "文件类型错误");
			}
			
			File diskFile=new File(path+fileName);
			if (!diskFile.exists()) { // 如果路径不存在，创建
				diskFile.mkdirs();
			}
			file.transferTo(diskFile);
		} catch (IllegalStateException e) {
			e.printStackTrace();
			return new Result(0, "图片上传失败");
		} catch (IOException e) {
			e.printStackTrace();
			return new Result(0, "图片上传失败");
		}
		Image image=new Image();
		image.setUrl(fileName);
		image.setState(2);
		image.setUser(user.getId());
		im.insert(image);
		user.setImage(image.getId());
		Result result=ubiz.updateUserImage(user);
		return result;
	}
	
	@ResponseBody
	@PostMapping("updateDefault.do")
	public Result updateUserDefaultimage(Model m,@SessionAttribute("loginUser") User user,Integer id){
		if(id!=null){
			user.setImage(id);
		}
		Result result=ubiz.updateUserImage(user);
		return result;
	}
	
	@ModelAttribute
	public void searchCar(@SessionAttribute("loginUser") User user,Model m) {
		if(user != null) {
			List<Car> clist=cbiz.findCar(user);
			m.addAttribute("clist", clist);
		}
	}
}
